package com.basketball.gym.utils;

import com.alibaba.fastjson.JSON;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import com.aliyun.teaopenapi.models.Config;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;

@Service
@Setter
@ConfigurationProperties(prefix = "oss")
public class AliYunService {
    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;
    private static String DATABASE = "home";
    private static String UPDATE_FACE_ENTITY = "UpdateFaceEntity";
    private static String ADD_FACE_ENTITY = "AddFaceEntity";
    private static String BIND_PHONE="SMS_215072955";
    private static String UPDATE_PHONE="SMS_215072954";
    private static String SIGN_NAME="跃动体育篮球中心";

    public String getUrl(String objectName) {
        return "https://" + bucketName + "." + endpoint + "/" + objectName;
    }

    /**
     * 上传文件
     */
    public String uploadFile(InputStream inputStream, String objectName) {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 上传网络流。
        ossClient.putObject(bucketName, objectName, inputStream);

        // 关闭OSSClient。
        ossClient.shutdown();
        return "https://" + bucketName + "." + endpoint + "/" + objectName;
    }

    /**
     * 获取绑定手机号的验证码
     */
    public String getBindPhoneCode(String phone){
        com.aliyun.dysmsapi20170525.Client client = null;
        try {
            Code code = new Code();
            String codeJson = JSON.toJSONString(code);
            client = createClient();
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(phone)
                    .setSignName(SIGN_NAME)
                    .setTemplateCode(BIND_PHONE)
                    .setTemplateParam(codeJson);
            SendSmsResponse sendSmsResponse=client.sendSms(sendSmsRequest);
            System.out.println(JSON.toJSONString(sendSmsResponse.getBody()));
            if(sendSmsResponse.getBody().message.equals("OK")){
                return code.getStringCode();
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 获取修改手机号的验证码
     */
    public String getUpdatePhoneCode(String phone){
        com.aliyun.dysmsapi20170525.Client client = null;
        try {
            Code code = new Code();
            String codeJson = JSON.toJSONString(code);
            client = createClient();
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(phone)
                    .setSignName(SIGN_NAME)
                    .setTemplateCode(UPDATE_PHONE)
                    .setTemplateParam(codeJson);
            SendSmsResponse sendSmsResponse=client.sendSms(sendSmsRequest);
            System.out.println(JSON.toJSONString(sendSmsResponse.getBody()));
            if(sendSmsResponse.getBody().message.equals("OK")){
                return code.getStringCode();
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    class Code {
        private Integer code;
        private String phone;
        private String number;

        public Code() {
            code = randCode();
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getStringCode() {
            return new String(String.valueOf(code));
        }
    }

    private Integer randCode() {
        return (int) ((Math.random() * 9 + 1) * 100000);
    }

}
